
package server.cashshop;


import com.alibaba.druid.pool.DruidPooledConnection;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import provider.*;
import tools.Pair;

import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
















 public class CashItemFactory {
    /*  19 */   private static final Logger log = LogManager.getLogger(CashItemFactory.class.getName());
    /*  20 */   private static final int[] bestItems = new int[]{30200045, 50000080, 30200066, 50400016, 30100092};
    /*  21 */   private static final CashItemFactory instance = new CashItemFactory();
    /*  22 */   private final List<CashItemCategory> categories = new LinkedList<>();
    /*  23 */   private final Map<Integer, CashItemInfo.CashModInfo> itemMods = new HashMap<>();
    /*  24 */   private final Map<Integer, CashItemForSql> menuItems = new HashMap<>();
    /*  25 */   private final Map<Integer, CashItemForSql> categoryItems = new HashMap<>();
    /*  26 */   private final Map<Integer, CashItemInfo> itemStats = new HashMap<>();
    /*  27 */   private final Map<Integer, Integer> idLookup = new HashMap<>();
    /*  28 */   private final Map<Integer, CashItemInfo> oldItemStats = new HashMap<>();
    /*  29 */   private final Map<Integer, Integer> oldIdLookup = new HashMap<>();
    /*  30 */   private final Map<Integer, List<Integer>> itemPackage = new HashMap<>();
    /*  31 */   private final Map<Integer, List<Pair<Integer, Integer>>> openBox = new HashMap<>();
    /*  32 */   private final MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Etc.wz"));
    /*  33 */   private final MapleData commodities = this.data.getData("Commodity.img");
    /*  34 */   private final List<Integer> blockRefundableItemId = new LinkedList<>();
    /*  35 */   private final Map<Integer, Byte> baseNewItems = new HashMap<>();



    public static CashItemFactory getInstance() {
        /*  38 */
        return instance;

    }



    public void initialize() {
        /*  42 */
        this.blockRefundableItemId.clear();
        /*  43 */
        int onSaleSize = 0;
        /*  44 */
        Map<Integer, Integer> fixId = new HashMap<>();

        /*  46 */
        for (MapleData field : this.commodities.getChildren()) {
            /*  47 */
            int SN = MapleDataTool.getIntConvert("SN", field, 0);
            /*  48 */
            int itemId = MapleDataTool.getIntConvert("ItemId", field, 0);
            /*  49 */
            int count = MapleDataTool.getIntConvert("Count", field, 1);
            /*  50 */
            int price = MapleDataTool.getIntConvert("Price", field, 0);
            /*  51 */
            int meso = MapleDataTool.getIntConvert("Meso", field, 0);
            /*  52 */
            int originalPrice = MapleDataTool.getIntConvert("originalPrice", field, 0);
            /*  53 */
            int period = MapleDataTool.getIntConvert("Period", field, 0);
            /*  54 */
            int gender = MapleDataTool.getIntConvert("Gender", field, 2);
            /*  55 */
            byte csClass = (byte) MapleDataTool.getIntConvert("Class", field, 0);
            /*  56 */
            byte priority = (byte) MapleDataTool.getIntConvert("Priority", field, 0);
            /*  57 */
            int termStart = MapleDataTool.getIntConvert("termStart", field, 0);
            /*  58 */
            int termEnd = MapleDataTool.getIntConvert("termEnd", field, 0);
            /*  59 */
            boolean onSale = (MapleDataTool.getIntConvert("OnSale", field, 0) > 0 || isOnSalePackage(SN));
            /*  60 */
            boolean bonus = (MapleDataTool.getIntConvert("Bonus", field, 0) > 0);
            /*  61 */
            boolean refundable = (MapleDataTool.getIntConvert("Refundable", field, 0) == 0);
            /*  62 */
            boolean discount = (MapleDataTool.getIntConvert("discount", field, 0) > 0);
            /*  63 */
            if (onSale) {
                /*  64 */
                onSaleSize++;

            }
            /*  66 */
            CashItemInfo stats = new CashItemInfo(itemId, count, price, originalPrice, meso, SN, period, gender, csClass, priority, termStart, termEnd, onSale, bonus, refundable, discount);
            /*  67 */
            if (SN > 0) {
                /*  68 */
                this.itemStats.put(Integer.valueOf(SN), stats);
                /*  69 */
                if (this.idLookup.containsKey(Integer.valueOf(itemId))) {
                    /*  70 */
                    fixId.put(Integer.valueOf(SN), Integer.valueOf(itemId));
                    /*  71 */
                    this.blockRefundableItemId.add(Integer.valueOf(itemId));

                }
                /*  73 */
                this.idLookup.put(Integer.valueOf(itemId), Integer.valueOf(SN));

            }

        }
        /*  76 */
        log.info("共加载 " + this.itemStats.size() + " 个商城道具 有 " + onSaleSize + " 个道具处于出售状态...");
        /*  77 */
        log.info("其中有 " + fixId.size() + " 重复价格的道具和 " + this.blockRefundableItemId.size() + " 个禁止换购的道具.");

        /*  79 */
        MapleData packageData = this.data.getData("CashPackage.img");
        /*  80 */
        for (MapleData mapleData : packageData.getChildren()) {
            /*  81 */
            if (mapleData.getChildByPath("SN") == null) {

                continue;

            }
            /*  84 */
            List<Integer> packageItems = new ArrayList<>();
            /*  85 */
            for (MapleData dat : mapleData.getChildByPath("SN").getChildren()) {
                /*  86 */
                packageItems.add(Integer.valueOf(MapleDataTool.getIntConvert(dat)));

            }
            /*  88 */
            this.itemPackage.put(Integer.valueOf(Integer.parseInt(mapleData.getName())), packageItems);

        }
        /*  90 */
        log.info("共加载 " + this.itemPackage.size() + " 个商城礼包...");

        /*  92 */
        onSaleSize = 0;
        /*  93 */
        MapleDataDirectoryEntry root = this.data.getRoot();
        /*  94 */
        for (MapleDataEntry topData : root.getFiles()) {
            /*  95 */
            if (topData.getName().startsWith("OldCommodity")) {
                /*  96 */
                MapleData Commodity = this.data.getData(topData.getName());
                /*  97 */
                for (MapleData field : Commodity.getChildren()) {
                    /*  98 */
                    int SN = MapleDataTool.getIntConvert("SN", field, 0);
                    /*  99 */
                    int itemId = MapleDataTool.getIntConvert("ItemId", field, 0);
                    /* 100 */
                    int count = MapleDataTool.getIntConvert("Count", field, 1);
                    /* 101 */
                    int price = MapleDataTool.getIntConvert("Price", field, 0);
                    /* 102 */
                    int meso = MapleDataTool.getIntConvert("Meso", field, 0);
                    /* 103 */
                    int originalPrice = MapleDataTool.getIntConvert("originalPrice", field, 0);
                    /* 104 */
                    int period = MapleDataTool.getIntConvert("Period", field, 0);
                    /* 105 */
                    int gender = MapleDataTool.getIntConvert("Gender", field, 2);
                    /* 106 */
                    byte csClass = (byte) MapleDataTool.getIntConvert("Class", field, 0);
                    /* 107 */
                    byte priority = (byte) MapleDataTool.getIntConvert("Priority", field, 0);
                    /* 108 */
                    int termStart = MapleDataTool.getIntConvert("termStart", field, 0);
                    /* 109 */
                    int termEnd = MapleDataTool.getIntConvert("termEnd", field, 0);
                    /* 110 */
                    boolean onSale = (MapleDataTool.getIntConvert("OnSale", field, 0) > 0 || isOnSalePackage(SN));
                    /* 111 */
                    boolean bonus = (MapleDataTool.getIntConvert("Bonus", field, 0) >= 0);
                    /* 112 */
                    boolean refundable = (MapleDataTool.getIntConvert("Refundable", field, 0) == 0);
                    /* 113 */
                    boolean discount = (MapleDataTool.getIntConvert("discount", field, 0) >= 0);
                    /* 114 */
                    if (onSale) {
                        /* 115 */
                        onSaleSize++;

                    }
                    /* 117 */
                    CashItemInfo stats = new CashItemInfo(itemId, count, price, originalPrice, meso, SN, period, gender, csClass, priority, termStart, termEnd, onSale, bonus, refundable, discount);
                    /* 118 */
                    if (SN > 0) {
                        /* 119 */
                        this.oldItemStats.put(Integer.valueOf(SN), stats);
                        /* 120 */
                        this.oldIdLookup.put(Integer.valueOf(itemId), Integer.valueOf(SN));

                    }

                }

            }

        }
        /* 125 */
        log.info("共加载 " + this.oldItemStats.size() + " 个老的商城道具 有 " + onSaleSize + " 个道具处于出售状态...");

        /* 127 */
        loadMoifiedItemInfo();
        /* 128 */
        loadRandomItemInfo();

        /* 130 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
            /* 131 */
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_categories"); ResultSet rs = ps.executeQuery()) {
                /* 132 */
                while (rs.next()) {
                    /* 133 */
                    CashItemCategory cat = new CashItemCategory(rs.getInt("categoryid"), rs.getString("name"), rs.getInt("parent"), rs.getInt("flag"), rs.getInt("sold"));
                    /* 134 */
                    this.categories.add(cat);

                }
                /* 136 */
            } catch (SQLException e) {
                /* 137 */
                log.error("Failed to load cash shop categories. ", e);

            }

            /* 140 */
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_menuitems"); ResultSet rs = ps.executeQuery()) {
                /* 141 */
                while (rs.next()) {
                    /* 142 */
                    CashItemForSql item = new CashItemForSql(rs.getInt("category"), rs.getInt("subcategory"), rs.getInt("parent"), rs.getString("image"), rs.getInt("sn"), rs.getInt("itemid"), rs.getInt("flag"), rs.getInt("price"), rs.getInt("discountPrice"), rs.getInt("quantity"), rs.getInt("expire"), rs.getInt("gender"), rs.getInt("likes"));
                    /* 143 */
                    this.menuItems.put(Integer.valueOf(item.getSN()), item);

                }
                /* 145 */
            } catch (SQLException e) {
                /* 146 */
                log.error("Failed to load cash shop menuitems. ", e);

            }

            /* 149 */
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_items"); ResultSet rs = ps.executeQuery()) {
                /* 150 */
                while (rs.next()) {
                    /* 151 */
                    CashItemForSql item = new CashItemForSql(rs.getInt("category"), rs.getInt("subcategory"), rs.getInt("parent"), rs.getString("image"), rs.getInt("sn"), rs.getInt("itemid"), rs.getInt("flag"), rs.getInt("price"), rs.getInt("discountPrice"), rs.getInt("quantity"), rs.getInt("expire"), rs.getInt("gender"), rs.getInt("likes"));
                    /* 152 */
                    this.categoryItems.put(Integer.valueOf(item.getSN()), item);

                }

            }
            /* 155 */
        } catch (SQLException e) {
            /* 156 */
            log.error("Failed to load cash shop items. ", e);

        }

    }



    public boolean isOnSalePackage(int snId) {
        /* 161 */
        return (snId >= 170200002 && snId <= 170200013);

    }



    public void loadMoifiedItemInfo() {
        /* 165 */
        this.itemMods.clear();
        /* 166 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection();
                /* 167 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_modified_items"); ResultSet rs = ps.executeQuery()) {
            /* 168 */
            while (rs.next()) {



















                /* 188 */
                CashItemInfo.CashModInfo ret = new CashItemInfo.CashModInfo(rs.getInt("serial"), rs.getInt("discount_price"), rs.getInt("mark"), (rs.getInt("showup") > 0), rs.getInt("itemid"), rs.getInt("priority"), (rs.getInt("package") > 0), rs.getInt("period"), rs.getInt("gender"), rs.getInt("count"), rs.getInt("meso"), rs.getInt("csClass"), rs.getInt("termStart"), rs.getInt("termEnd"), rs.getInt("extra_flags"), rs.getInt("fameLimit"), rs.getInt("levelLimit"), rs.getInt("categories"), (rs.getByte("bast_new") > 0));
                /* 189 */
                this.itemMods.put(Integer.valueOf(ret.getSn()), ret);
                /* 190 */
                CashItemInfo cc = this.itemStats.get(Integer.valueOf(ret.getSn()));
                /* 191 */
                if (cc != null) {
                    /* 192 */
                    ret.toCItem(cc);
                    /* 193 */
                    if (ret.isBase_new()) {
                        /* 194 */
                        this.baseNewItems.put(Integer.valueOf(ret.getSn()), Byte.valueOf(cc.getCsClass()));

                    }

                }


            }
            /* 199 */
        } catch (SQLException e) {
            /* 200 */
            log.error("cashshop_modified_items_error: ", e);

        }
        /* 202 */
        log.info("共加载了 " + this.itemMods.size() + " 个商品");

    }



    public void loadRandomItemInfo() {
        /* 206 */
        this.openBox.clear();
        /* 207 */
        try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection();
                /* 208 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_randombox"); ResultSet rs = ps.executeQuery()) {
            /* 209 */
            while (rs.next()) {
                /* 210 */
                List<Pair<Integer, Integer>> boxItems = this.openBox.computeIfAbsent(Integer.valueOf(rs.getInt("randboxID")), integer -> new ArrayList());
                /* 211 */
                boxItems.add(new Pair(Integer.valueOf(rs.getInt("itemSN")), Integer.valueOf(rs.getInt("count"))));

            }

            /* 214 */
        } catch (SQLException e) {
            /* 215 */
            log.error("加载商城随机箱子的信息出错.", e);

        }

        /* 218 */
        log.info("共加载 " + this.openBox.size() + " 个商城随机箱子的信息...");

    }



    public CashItemInfo getSimpleItem(int sn) {
        /* 222 */
        return this.itemStats.get(Integer.valueOf(sn));

    }



    public Map<Integer, CashItemInfo> getAllItem() {
        /* 226 */
        return this.itemStats;

    }



    public boolean isBlockRefundableItemId(int itemId) {
        /* 230 */
        return this.blockRefundableItemId.contains(Integer.valueOf(itemId));

    }



    public CashItemInfo.CashModInfo getModInfo(int sn) {
        /* 234 */
        return this.itemMods.get(Integer.valueOf(sn));

    }



    public Map<Integer, CashItemInfo.CashModInfo> getAllModInfo() {
        /* 238 */
        return this.itemMods;

    }



    public Map<Integer, Byte> getAllBaseNewInfo() {
        /* 242 */
        return this.baseNewItems;

    }



    public CashItemInfo getItem(int sn) {
        /* 246 */
        return getItem(sn, true);

    }



    public CashItemInfo getItem(int sn, boolean checkSale) {
        /* 250 */
        CashItemInfo stats = this.itemStats.get(Integer.valueOf(sn));
        /* 251 */
        CashItemInfo.CashModInfo z = getModInfo(sn);
        /* 252 */
        if (z != null && z.isShowUp()) {
            /* 253 */
            return z.toCItem(stats);

        }
        /* 255 */
        if (stats == null) {
            /* 256 */
            return null;

        }
        /* 258 */
        return (checkSale && !stats.onSale()) ? null : stats;

    }



    public CashItemForSql getMenuItem(int sn) {
        /* 262 */
        for (CashItemForSql ci : getMenuItems()) {
            /* 263 */
            if (ci.getSN() == sn) {
                /* 264 */
                return ci;

            }

        }
        /* 267 */
        return null;

    }



    public CashItemForSql getAllItem(int sn) {
        /* 271 */
        for (CashItemForSql ci : getAllItems()) {
            /* 272 */
            if (ci.getSN() == sn) {
                /* 273 */
                return ci;

            }

        }
        /* 276 */
        return null;

    }



    public List<Integer> getPackageItems(int itemId) {
        /* 280 */
        return this.itemPackage.get(Integer.valueOf(itemId));

    }






    public Map<Integer, List<Pair<Integer, Integer>>> getRandomItemInfo() {
        /* 287 */
        return this.openBox;

    }



    public boolean hasRandomItem(int itemId) {
        /* 291 */
        return this.openBox.containsKey(Integer.valueOf(itemId));

    }



    public List<Pair<Integer, Integer>> getRandomItem(int itemId) {
        /* 295 */
        return this.openBox.get(Integer.valueOf(itemId));

    }



    public int[] getBestItems() {
        /* 299 */
        return bestItems;

    }



    public int getLinkItemId(int itemId) {
        /* 303 */
        switch (itemId) {

            case 5000029:

            case 5000030:

            case 5000032:

            case 5000033:

            case 5000035:
                /* 309 */
                return 5000028;

            case 5000048:

            case 5000049:

            case 5000050:

            case 5000051:

            case 5000052:
                /* 315 */
                return 5000047;

        }
        /* 317 */
        return itemId;

    }



    public int getSnFromId(int itemId) {
        /* 321 */
        if (this.idLookup.containsKey(Integer.valueOf(itemId))) {
            /* 322 */
            return ((Integer) this.idLookup.get(Integer.valueOf(itemId))).intValue();

        }
        /* 324 */
        return 0;

    }



    public List<CashItemCategory> getCategories() {
        /* 328 */
        return this.categories;

    }



    public List<CashItemForSql> getMenuItems(int type) {
        /* 332 */
        List<CashItemForSql> items = new LinkedList<>();
        /* 333 */
        for (CashItemForSql ci : this.menuItems.values()) {
            /* 334 */
            if (ci.getSubCategory() / 10000 == type) {
                /* 335 */
                items.add(ci);

            }

        }
        /* 338 */
        return items;

    }



    public List<CashItemForSql> getMenuItems() {
        /* 342 */
        List<CashItemForSql> items = new LinkedList<>();
        /* 343 */
        for (CashItemForSql ci : this.menuItems.values()) {
            /* 344 */
            items.add(ci);

        }
        /* 346 */
        return items;

    }



    public List<CashItemForSql> getAllItems(int type) {
        /* 350 */
        List<CashItemForSql> items = new LinkedList<>();
        /* 351 */
        for (CashItemForSql ci : this.categoryItems.values()) {
            /* 352 */
            if (ci.getSubCategory() / 10000 == type) {
                /* 353 */
                items.add(ci);

            }

        }
        /* 356 */
        return items;

    }



    public List<CashItemForSql> getAllItems() {
        /* 360 */
        List<CashItemForSql> items = new LinkedList<>();
        /* 361 */
        for (CashItemForSql ci : this.categoryItems.values()) {
            /* 362 */
            items.add(ci);

        }
        /* 364 */
        return items;

    }



    public List<CashItemForSql> getCategoryItems(int subcategory) {
        /* 368 */
        List<CashItemForSql> items = new LinkedList<>();
        /* 369 */
        for (CashItemForSql ci : this.categoryItems.values()) {
            /* 370 */
            if (ci.getSubCategory() == subcategory) {
                /* 371 */
                items.add(ci);

            }

        }
        /* 374 */
        return items;

    }

}


/* Location:              C:\PGX\Server.jar!\server\cashshop\CashItemFactory.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */